package com.example.hotel.controller;



import com.example.hotel.pojo.Result;
import com.example.hotel.pojo.User;
import com.example.hotel.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/test")
public class testController {

    //GetMapping测试
    @GetMapping("/test1")
    public String test1(){
        return "Hello test1";
    }
    @GetMapping("/test2")
    public String test2(){
        return "Hello test2";
    }
    @GetMapping("/test3")
    public Result<String> test3(){
        return Result.success("test3");
    }
    @GetMapping("/test4")
    public Result test4(){
        return Result.success();
    }

    @GetMapping("test5")
    public Result<String> test5(@RequestHeader(name = "Authorization")String token){
        return Result.success(token);
    }

    @RequestMapping(value = "/test6",method = RequestMethod.GET)
    public Result<String>test6(){
        return Result.success("test6 success");
    }
    //PutMapping测试
    @PutMapping("/test7/{id}")
    public Result<String>test7(@PathVariable("id") String id){
        return Result.success("id: "+id+" test7 success");
    }
    @RequestMapping ("/error")
    @ResponseStatus(value = HttpStatus.NOT_FOUND,reason = "You visit this error page")
    public void test8(){
        System.out.println("error");
    }

    //@RequestBody测试
    //@Valid测试
    @PostMapping("/testlogin")
    public Result<String>login(@Valid @RequestBody User user){
        //@Valid注解:传入的值要经过校验,校验规则在实体类中规定
        System.out.println(user);
        return Result.success(user.getUsername());
    }

    //用户访问资源，登录验证测试,token令牌验证
    @GetMapping("/resource")
    public Result<String>get_resource(@RequestHeader(name="Authorization")String token, HttpServletResponse response){
        try {
            //token令牌 验证
            //Map<String,Object> claims = JwtUtil.parseToken(token);
            //从线程中获取claims
            Map<String,Object> claims= ThreadLocalUtil.get();

            System.out.println(claims);
            return Result.success("user " +claims.get("username")+ " visit resource sucessful.");
        }
        catch(Exception e){
            response.setStatus(401);
            return Result.error(" user not login");
        }
    }
    @GetMapping("/getroomlist")
    public Result<String>get_roomlist(@RequestHeader(name="Authorization")String token, HttpServletResponse response,@RequestParam("roomtype") String roomtype){
        try {
            //token令牌 验证
            //Map<String,Object> claims = JwtUtil.parseToken(token);
            //从线程中获取claims
            Map<String,Object> claims= ThreadLocalUtil.get();

            System.out.println(claims);
            return Result.success("user " +claims.get("username")+ " visit "+roomtype+" resource sucessful.");
        }
        catch(Exception e){
            response.setStatus(401);
            return Result.error(" user not login");
        }
    }
}
